import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '@/views/home'
import Search from '@/views/search/index.vue'
import Category from '@/views/category/index.vue'
import Product from '@/views/product/index.vue'
import SearchList from '@/views/search/searchList/index.vue'
import { get } from '@/Storage/index.js'

Vue.use(VueRouter)

const routes = [
  // 首页
  {
    path: '/',
    component: Home,
    meta: {
      showFooter: true
    }
  },
  // 搜索页
  {
    path: '/search',
    name: 'Search',
    component: Search

  },
  // 商品搜索列表
  {
    path: '/list',
    name: 'List',
    component: SearchList
  },
  // 商品列表
  {
    path: '/category',
    component: Category,
    meta: {
      showFooter: true
    }
  },
  {
    path: '/product/:id',
    component: Product
  },
  // 购物车
  {
    path: '/shopcart',
    name: 'ShopCart',
    component: () => import('@/views/shopcart/index.vue')
  },
  // 用户路由
  {
    path: '/user',
    component: () => import('@/views/user/index.vue'),
    meta: {
      showFooter: true
    }
  },
  // 登入
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    redirect: '/login/password',
    children: [
      {
        path: 'password',
        component: () => import('@/views/login/password.vue')
      },
      {
        path: 'phone',
        component: () => import('@/views/login/phone.vue')
      }
    ]
  },
  // 注册
  {
    path: '/register',
    component: () => import('@/views/register/index.vue')
  },
  // 订单路由
  {
    path: '/order',
    component: () => import('@/views/order/index.vue'),
    meta: {
      showFooter: true
    }
  },
  {
    path: '/setorder',
    component: () => import('@/views/order/setorder.vue')
  },
  // 地址路由
  {
    path: '/address',
    component: () => import('@/views/address/index.vue'),
    redirect: '/address/addresslist',
    children: [
      {
        path: 'addAddress',
        name: 'add',
        component: () => import('@/views/address/addAddress.vue')
      },
      {
        path: 'addresslist',
        name: 'list',
        component: () => import('@/views/address/addresslist.vue')
      }
    ]
  },
  {
    path: '/pay/:id',
    component: () => import('@/views/payOrder/index.vue')
  },
  // 支付成功
  {
    path: '/succeed',
    component: () => import('@/views/succeed/index.vue')
  },
  // 用户中心
  {
    path: '/config',
    component: () => import('@/views/config')
  }
]

const router = new VueRouter({
  routes,
  scrollBehavior(to, from) {
    return { top: 0 }
  }
})
router.beforeEach((to, from, next) => {
  const token = get('token')
  // 用户登入成功
  if (token) {
    // 登入后不能再访问登入和注册页
    if (to.path === '/login/password' || to.path === '/login/phone') {
      next(false)
    } else {
      next()
    }
    // 没有登入
  } else {
    if (to.path === '/order' || to.path === '/setorder' || to.path === '/address' || to.path === '/address/addAddress' || to.path === '/address/addresslist' || to.path === '//pay/:id' || to.path === '/succeed' || to.path === '/config') {
      alert('请登入')
      next('/login')
    } else {
      next()
    }
  }
})
export default router
